package com.flying.poly.cast;

import com.flying.poly.Person;
import com.flying.poly.Student;

import javax.swing.*;

/**
 * @author 刘鹏飞
 * @Classname CastDemo
 * @Date 2024/12/13 1:30
 * @Created by 刘鹏飞
 * @Description 引用数据类型的类型转换
 */
public class CastDemo {

    public static void main(String[] args) {


        // 1. 报错，类型转换错误
/*
        Person p1 = new Person();
        Student s1= (Student)p1;
*/

        // 2.
        Person p2 = new Student();
        Student s22=(Student) p2;
        s22.study();

        // 3. 使用 instanceof 运算符比较对象和类之间的关系
        Person p3=new Person();
        Person p4= new Student();
        Student s1=new Student();

        System.out.println(p3 instanceof Person);   // true
        System.out.println(p3 instanceof Student);  // false
        System.out.println(p4 instanceof Person);   //  true
        System.out.println(p4 instanceof Student);  //  true
        System.out.println(s1 instanceof Person);   // true
        System.out.println(s1 instanceof Student);  // true
        System.out.println("----------------");


        //  两个对象之间的比较
        // TODO: 比较器 ： 后续复习
        Person p5=new Person("jack",18);
        Person p6=new Person("jack",18);
        System.out.println(p5.equals(p6));    // true

        Student s2=new Student(1,"jack",18);
        System.out.println(p5.equals(s2));   //  true

        Student s3=new Student(2,"jack",15);
        System.out.println(p5.equals(s3));   //  fasle

        Student s4=new Student(2,"alice",10);
        System.out.println(p5.equals(s3));   //  fasle





    }

}
